﻿package com.msjt.dao;

import java.util.List;

import com.msjt.base.BaseDAO;
import com.msjt.entity.News;
/**
 * name:NewsDAO
 * function:新闻信息dao
 * author: kw
 * time:2013.8.26
 * updateTime:2013.9.1
 */
public class NewsDAO extends BaseDAO {
	//后台新闻列表，分页显示
	@SuppressWarnings("unchecked")
	public List<News> getNewsAdmin(int pageIndex,int pageSize){
		String hql="select n from News as n order by n.newsType desc , n.newsDate desc";
		return (List<News>) this.searchByPage(hql, null, pageIndex, pageSize);
	}
	//前台新闻列表分页显示,需要新闻ID,新闻标题，新闻内容，新闻日期
	 @SuppressWarnings("unchecked")
	public List<News> getNewsMore(int pageIndex,int pageSize){
		 String hql="select new News(news.newsId,news.newsTitle,news.newContent,news.newsDate) from News as news order by news.newsDate desc";
		 return (List<News>) this.searchByPage(hql, null, pageIndex, pageSize);
	 }	
	//新闻总条数
	public Integer getNewsCount(){
		String hql="select count(n.newsId) from News as n";
		return this.getTotalCount(hql, null);
	}
	//获取5条新闻的ID及标题，按时间排序
	@SuppressWarnings("unchecked")
	public List<News> findFiveNewsTime(){
		//String hql="select top 5 newsId,newsTitle from News where newsType='未置顶' order by newsDate desc";
		String hql="select new News(newsId,newsTitle) from News as n where newsType='置顶' order by newsDate desc";
		return (List<News>) this.searchByPage(hql, null, 1, 5);
	}
	//获取未置顶的6条新闻
	@SuppressWarnings("unchecked")
	public List<News> findSixNewsTopTime(){
		//String hql="select top 6 newsId,newsTitle from News where newsType='置顶' order by newsDate desc";
		String hql="select new News(newsId,newsTitle) from News as n where newsType='未置顶' order by newsDate desc";
		return (List<News>) this.searchByPage(hql, null, 1, 6);
	}
	//根据ID查询新闻的详细信息
	public News getNewsInfo(News news){
		String hql="select news from News as news where newsId=?";
		Object [] params={news.getNewsId()};
		return (News) this.searchByHQL(hql, params).get(0);
	}
	//新闻置顶管理
	public boolean clickTop(News news){
		String queryString="update News as n set n.newsType=? where n.newsId=?";
		Object [] parameters={news.getNewsType(),news.getNewsId()};
		return this.updateByHql(queryString, parameters);
	}
	//增加新闻次数
	public boolean updateClickNum(News news){
		String queryString ="update News as n set n.newsClick=? where n.newsId=?";
		Object [] parameters={news.getNewsClick()+1,news.getNewsId()};
		return this.updateByHql(queryString, parameters);
	}
	//添加新闻
	public boolean addNews(News news){
		return this.save(news);
	}
	//修改新闻
	public boolean updateNews(News news){
		return this.saveOrUpdate(news);
	}
	//删除新闻
	public boolean deleteNews(News news){
		return this.deleteById(news.getNewsId());
	}
	//模糊查询根据条件计算总条数
	public Integer getNewsCount(News news){
		if(news.getNewsType()!=null&&news.getNewsType().equals("不限")&&news.getNewsTitle().equals("")||news.getNewsTitle()==null){
			return this.getNewsCount();
		}
		if(news.getNewsType()!=null&&news.getNewsType().equals("不限")&&!news.getNewsTitle().equals("")||news.getNewsTitle()==null){
			String hql="select count(n.newsId) from News as n where n.newsTitle like ?";
			Object [] params={"%"+news.getNewsTitle()+"%"};
			return this.getTotalCount(hql, params);
		}
		if(news.getNewsTitle()!=null&&(!news.getNewsTitle().equals(""))&&news.getNewsType()==null||news.getNewsType().equals("")){
			String hql="select count(n.newsId) from News as n where n.newsTitle like ?";
			Object [] params={"%"+news.getNewsTitle()+"%"};
			return this.getTotalCount(hql, params);
		}
		if(news.getNewsType()!=null&&(!news.getNewsType().equals(""))&&news.getNewsTitle().equals("")||news.getNewsTitle()==null){
			String hql="select count(n.newsId) from News as n where n.newsType like ?";
			Object [] params={"%"+news.getNewsType()+"%"};
			return this.getTotalCount(hql, params);
		}
		if(news.getNewsType()!=null&&news.getNewsType().equals("不限")&&news.getNewsTitle().equals("")||news.getNewsTitle()==null){
			return this.getNewsCount();
		}
		String hql="select count(n.newsId) from News as n where n.newsTitle like ? and n.newsType like ?";
		Object [] params={"%"+news.getNewsTitle()+"%","%"+news.getNewsType()+"%"};
		return this.getTotalCount(hql, params);
	}
	//模糊查询新闻
	@SuppressWarnings("unchecked")
	public List<News> selectNewsLike(News news,int pageIndex,int pageSize){
		if(news.getNewsType()!=null&&news.getNewsType().equals("不限")&&news.getNewsTitle().equals("")||news.getNewsTitle()==null){
			return this.getNewsAdmin(pageIndex,pageSize);
		}
		if(news.getNewsType()!=null&&news.getNewsType().equals("不限")&&!news.getNewsTitle().equals("")||news.getNewsTitle()==null){
			String hql="select n from News as n where n.newsTitle like ? order by n.newsType desc , n.newsDate desc";
			Object [] params={"%"+news.getNewsTitle()+"%"};
			return (List<News>) this.searchByPage(hql, params, pageIndex, pageSize);
		}
		if(news.getNewsTitle()!=null&&(!news.getNewsTitle().equals(""))&&news.getNewsType().equals("")||news.getNewsType()==null){
			String hql="select n from News as n where n.newsTitle like ? order by n.newsType desc , n.newsDate desc";
			Object [] params={"%"+news.getNewsTitle()+"%"};
			return (List<News>) this.searchByPage(hql, params, pageIndex, pageSize);
		}
		if(news.getNewsType()!=null&&(!news.getNewsType().equals(""))&&news.getNewsTitle().equals("")||news.getNewsTitle()==null){
			String hql="select n from News as n where n.newsType=? order by n.newsType desc , n.newsDate desc";
			Object [] params={news.getNewsType()};
			return (List<News>) this.searchByPage(hql, params, pageIndex, pageSize);
		}
		String hql="select n from News as n where n.newsTitle like ? and n.newsType=?";
		Object [] params={"%"+news.getNewsTitle()+"%",news.getNewsType()};
		return (List<News>) this.searchByPage(hql, params, pageIndex, pageSize);
	}
	
	public boolean deleteMore(String idString){
		String hql="delete News as news where news.newsId in ("+idString+")";
		return this.updateByHql(hql, null);
	}
}
